package com.bk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bk.domain.User;
import com.bk.service.UserService;
import com.bk.mapper.UserMapper;
import com.bk.utils.PwdUtil;
import com.bk.utils.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 24940
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-04-04 11:19:56
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Override
    public Result login(User user, HttpServletRequest request, HttpServletResponse response) {
        String username = user.getUsername();
        User query = getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (query == null) {
            return Result.error("400", "用户名或密码错误");
        }
        String password = query.getPassword();
        String salt = query.getSalt();
        // 验证密码是否正确
        if (!PwdUtil.verify(user.getPassword(), salt, password)) {
            return Result.error("400", "用户名或密码错误");
        }
        if (user.isKeepSession()) {
            Cookie cookie = new Cookie("remember-me", username);
            cookie.setMaxAge(60 * 60 * 24 * 7);
            response.addCookie(cookie);
        }
        query.setPassword(null);
        query.setSalt(null);
        HttpSession session = request.getSession();
        session.setAttribute("user", query);
        session.setMaxInactiveInterval(60 * 60 * 24 * 7);
        return Result.success(query);
    }

    @Override
    public Result register(User user, HttpServletRequest request) {
        if (user.getCheckPassword() == null || !user.getCheckPassword().equals(user.getPassword())) {
            return Result.error("400", "两次密码输入不一致");
        }
        String username = user.getUsername();
        if (getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username)) != null) {
            return Result.error("400", "用户名已存在");
        }
        user.setSalt(PwdUtil.generateSalt());
        user.setPassword(PwdUtil.encrypt(user.getPassword(), user.getSalt()));
        return save(user) ? Result.success() : Result.error("400", "注册失败");
    }

    @Override
    public Result updateUserInfo(HttpServletRequest request, User user) {
        return updateById(user)? Result.success() : Result.error("400", "修改失败");
    }

    @Override
    public Result getUserInfo(Long id, HttpServletRequest request) {
        User query = getOne(new LambdaQueryWrapper<User>().eq(User::getId, id));
        if (query != null) {
            query.setPassword(null);
            query.setSalt(null);
            return Result.success(query);
        }
        return Result.error("400", "用户不存在");
    }

    @Override
    public Result logout(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "remember-me=; Max-Age=0");
        return Result.success();
    }

    @Override
    public Result getByIds(List<Integer> ids, HttpServletRequest request) {
        List<User> list = getBaseMapper().selectBatchIds(ids);
        return Result.success(list);
    }
}




